package com.lemon.tuvi2015;

import java.util.ArrayList;

import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.CMD;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;

public class MainActivity extends Activity {
	Context mContext;
	private InterstitialAd mInterstitialAd;
	String LOG_TAG = "Admob";
	String admob = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = MainActivity.this;
		Bundle extras = getIntent().getExtras();
		// if (getIntent().hasExtra(TagName.TITLE))
		// title = extras.getString(TagName.TITLE);
		// if (getIntent().hasExtra(TagName.KEY)) {
		// content = extras.getString(TagName.KEY);
		// showContent(content);
		// }
		if (getIntent().hasExtra(TagName.KEY)) {
			if (extras.getString(TagName.KEY).equals(SplashActivity.START))
				showDialog();
		}
		((Button) findViewById(R.id.btn_congiap)).setOnClickListener(clickListener);
		((Button) findViewById(R.id.btn_horoscope)).setOnClickListener(clickListener);
		((Button) findViewById(R.id.btn_xongdat)).setOnClickListener(clickListener);
		((Button) findViewById(R.id.btn_vankhan)).setOnClickListener(clickListener);
		((Button) findViewById(R.id.btn_boinotruoi)).setOnClickListener(clickListener);
		((Button) findViewById(R.id.btn_smskute)).setOnClickListener(clickListener);
		admob = getString(R.string.admob_id_full);
		initAd(admob);
		new AsyncTaskadmobfullscreen().execute("");
	}

	private void initAd(String admobId) {
		// Create the InterstitialAd and set the adUnitId.
		mInterstitialAd = new InterstitialAd(MainActivity.this);
		// Defined in values/strings.xml
		mInterstitialAd.setAdUnitId(admobId);
		mInterstitialAd.setAdListener(new AdListener() {
			/**
			 * Called when an ad is clicked and about to return to the
			 * application.
			 */
			@Override
			public void onAdClosed() {
				Log.d(LOG_TAG, "onAdClosed");
				finish();
			}

			/** Called when an ad failed to load. */
			@Override
			public void onAdFailedToLoad(int error) {
				String message = "onAdFailedToLoad: " + getErrorReason(error);
				Log.d(LOG_TAG, message);
			}

			/**
			 * Called when an ad is clicked and going to start a new Activity
			 * that will leave the application (e.g. breaking out to the Browser
			 * or Maps application).
			 */
			@Override
			public void onAdLeftApplication() {
				Log.d(LOG_TAG, "onAdLeftApplication");
			}

			/**
			 * Called when an Activity is created in front of the app (e.g. an
			 * interstitial is shown, or an ad is clicked and launches a new
			 * Activity).
			 */
			@Override
			public void onAdOpened() {
				Log.d(LOG_TAG, "onAdOpened");
			}

			/** Called when an ad is loaded. */
			@Override
			public void onAdLoaded() {
				Log.d(LOG_TAG, "onAdLoaded");
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		RequestAdmob();
	}

	private void RequestAdmob() {
		AdRequest adRequest = new AdRequest.Builder().build();
		mInterstitialAd.loadAd(adRequest);
	}

	private void displayAd() {
		// Show the ad if it's ready. Otherwise toast and restart the game.
		if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
			mInterstitialAd.show();
		} else {
			Log.e("Admob", "InterstitialAd: Ad did not load");
			finish();
		}
	}

	void showDialog() {
		new AlertDialog.Builder(mContext).setTitle("Thông báo").setMessage(mContext.getResources().getString(R.string.thongbao))
				.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				}).setIcon(android.R.drawable.ic_dialog_alert).show();
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_congiap:
				Intent intent = new Intent(getApplicationContext(), ZodiacActivity.class);
				intent.putExtra(TagName.KEY, CMD.MENU_CON_GIAP);
				startActivity(intent);
				break;
			case R.id.btn_horoscope:
				startActivity(new Intent(getApplicationContext(), HoroscopeActivity.class));
				break;
			case R.id.btn_xongdat:
				startActivity(new Intent(getApplicationContext(), XongdatActivity.class));
				break;
			case R.id.btn_vankhan:
				Intent intent2 = new Intent(getApplicationContext(), VankhanActivity.class);
				intent2.putExtra(TagName.KEY, CMD.MENU_VAN_KHAN);
				startActivity(intent2);
				break;
			case R.id.btn_boinotruoi:
				Intent intent3 = new Intent(getApplicationContext(), BoiActivity.class);
				intent3.putExtra(TagName.KEY, CMD.MENU_BOI_VUI);
				startActivity(intent3);
				break;
			case R.id.btn_smskute:
				Intent intent4 = new Intent(getApplicationContext(), SmsKuteActivity.class);
				intent4.putExtra(TagName.KEY, CMD.MENU_SMSKUTE);
				startActivity(intent4);
				break;
			}
		}
	};

	/** Gets a string error reason from an error code. */
	private String getErrorReason(int errorCode) {
		String errorReason = "";
		switch (errorCode) {
		case AdRequest.ERROR_CODE_INTERNAL_ERROR:
			errorReason = "Internal error";
			break;
		case AdRequest.ERROR_CODE_INVALID_REQUEST:
			errorReason = "Invalid request";
			break;
		case AdRequest.ERROR_CODE_NETWORK_ERROR:
			errorReason = "Network Error";
			break;
		case AdRequest.ERROR_CODE_NO_FILL:
			errorReason = "No fill";
			break;
		}
		return errorReason;
	}

	@Override
	public void onBackPressed() {
		displayAd();
	};

	public class AsyncTaskadmobfullscreen extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.getAdmobID();
			BaseEngine.showLogformLongString("", str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					ArrayList<String> arrayList = JsonParser.readAmobID(result);
					if (arrayList != null) {
						String adm = arrayList.get(1);
						if (!adm.equals(admob)) {
							initAd(arrayList.get(1));
							RequestAdmob();
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
